import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from 'axios'
import global from "@/../static/global.vue"

// import Logo from '../views/Logo.vue'

Vue.use(VueRouter)

const routes = [

  /* {
     path: '/',
     name: 'login',
     component: ()=>import("../views/Login.vue"),
     meta:{
       title:"登录"
     }
   },*/
  //
  {
    path: '*',
    redirect: '/sysDescription',
    meta: {
      title: "dashboard"
    }
  },
  // {
  //   path: '/dashBoard',
  //   meta: {
  //     title: "dashBoard"
  //   },
  //   component: () => import('../views/Dashboard.vue')
  // },
  {
    path: '/sysDescription',
    meta: {
      title: '首页',
      requireAuth:true
    },
    component: () => import('../views/Dashboard.vue')
  },
  {
    path: '/historicalalarm',
    name: 'HistoricalAlarm',
    meta:{
      title:"历史告警",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/HistoricalAlarm.vue')
  },
  {
    path: '/unhandledalert',
    name: 'UnhandledAlert',
    meta:{
      title:"未处理告警",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/UnhandledAlert.vue')
  },
  {
    path: '/hostSetting',
    name: 'HostSetting',
    meta:{
      title:"设备配置",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/HostSetting.vue')
  },
  {
    path: '/monitorOverview',
    name: 'MonitorOverview',
    meta:{
      title:"监控设备总览",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/MonitorOverview.vue')
  },
  {
    path: '/highLoadHostList',
    name: 'highLoadHostList',
    meta:{
      title:"高负载设备列表",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/HighLoadHostList.vue')
  },
  {
    path: '/detailedFlowChart',
    name: 'detailedFlowChart',
    meta: {
      title: '实时流量图',
      requireAuth:true
    },
    component: () => import('../views/DetailedFlowChart.vue')
  },
  {
    path: '/logDetails',
    name: 'logDetails',
    meta:{
      title: "日志详情",
      requireAuth:true
    },
    component: () => import('../views/LogDetails.vue')
  },
  // {
  //   path: '/basicMonitoringEquipment',
  //   name: 'BasicMonitoringEquipment',
  //   meta:{
  //     title:"基本监控设备"
  //   },
  //   component: () => import(/* webpackChunkName: "about" */ '../views/BasicMonitoringEquipment.vue')
  // },

  {
    path: '/basicMonitoring',
    name: 'BasicMonitoring',
    meta:{
      title:"基本监控设备",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/BasicMonitoring.vue')
  },
  {
    path: '/drawItemCharts',
    name: 'DrawItemCharts',
    meta: {
      title: '监控项实时数据',
      requireAuth:true
    },
    component: () => import('../views/DrawItemCharts.vue')
  },
  {
    path: '/itemInfo',
    name: 'ItemInfo',
    meta:{
      title:"监控项配置",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/ItemInfo.vue')
  },


  {
    path: '/triggerSetting',
    name: 'TriggerSetting',
    meta:{
      title:"告警项配置",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/TriggerSetting.vue')
  },
  {
    path: '/hostManagement',
    name: 'HostManagement',
    meta:{
      title:"主机管理",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/HostManagement.vue')
  },
  {
    path: '/topoEngineRoom',
    name: 'TopoEngineRoom',
    meta:{
      title:"机房预览",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/topology/TopoEngineRoom.vue')
  },
  {
    path: '/topoCabinet/:currentEngineRoom/:currentRoomNo',
    name: 'TopoCabinet',
    props: true,
    meta:{
      title:"机柜预览",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/topology/TopoCabinet.vue')
  },
  {
    path: '/topoMapEdit',
    name: 'TopoMapEdit',
    meta:{
      title:"拓扑编辑",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/topology/TopoMapEdit.vue')
  },
  {
    path: '/topoMenu',
    name: 'TopoMenu',
    meta: {
      title: "拓扑菜单",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/topology/TopoMenu.vue')
  },
  // {
  //   path: '/topoMenuOne',
  //   name: 'TopoMenuOne',
  //   meta: {
  //     title: "拓扑菜单1"
  //   },
  //   component: () => import(/* webpackChunkName: "about" */ '../views/topology/TopoMenuOne.vue')
  // },
  {
    path: '/topoSetting',
    name: 'TopoSetting',
    meta: {
      title: "拓扑配置",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/topology/TopoSetting.vue')
  },
  {
    path: '/topoViewShow',
    name: 'TopoViewShow.vue',
    meta: {
      title: "视图展示",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/topology/TopoViewShow.vue')
  },
  // {
  //   path: '/role',
  //   name: 'Role',
  //   meta:{
  //     title:"角色管理",
  //     requireAuth:true
  //   },
  //   component: () => import(/* webpackChunkName: "about" */ '../views/role.vue')
  // },
  // {
  //   path: '/user',
  //   name: 'User',
  //   meta:{
  //     title:"用户管理",
  //     requireAuth:true
  //   },
  //   component: () => import(/* webpackChunkName: "about" */ '../views/user.vue')
  // },
  {
    path: '/addTable',
    name: 'AddTable',
    meta:{
      title:"批量添加",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/AddTable.vue')
  },
  {
    path: '/ClassificationManagement',
    name: 'ClassificationManagement',
    meta: {
      title: "分类管理",
      requireAuth:true
    },
    component: () => import('../views/ClassificationManagement')
  },
  {
    path: '/knowledgeBaseManagement',
    name: 'KnowledgeBaseManagement',
    meta:{
      title:"知识库管理",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/knowledgeBase/KnowledgeBaseManagement.vue')
  },
  {
    path: '/knowledgeBaseInventory',
    name: 'KnowledgeBaseInventory',
    meta:{
      title:"知识库管理",
      requireAuth:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/knowledgeBase/KnowledgeBaseInventory.vue')
  },
  {
    path: '/performanceExcel',
    name: 'PerformanceExcel',
    meta:{
      title: "性能分析报表",
      requireAuth:true
    },
    component: () => import('../views/report/PerformanceExcel.vue')
  },
  {
    path: '/inspectionExcel',
    name: 'InspectionExcel',
    meta:{
      title: "巡检报告",
      requireAuth:true
    },
    component: () => import('../views/report/InspectionExcel.vue')
  },
  {
    path: '/statisticsExcel',
    name: 'StatisticsExcel',
    meta:{
      title: "资源统计报表",
      requireAuth:true
    },
    component: () => import('../views/report/StatisticsExcel.vue')
  },
  {
    path: '/alertExcel',
    name: 'AlertExcel',
    meta:{
      title: "告警统计报表",
      requireAuth:true
    },
    component: () => import('../views/report/AlertExcel.vue')
  },
  {
    path: '/businessManagement',
    name: 'BusinessManagement',
    meta:{
      title: "业务管理",
      requireAuth:true
    },
    component: () => import('../views/business/BusinessManagement.vue')
  },
  {
    path: '/databaseManagement',
    name: 'DatabaseManagement',
    meta:{
      title: "数据库管理",
      requireAuth:true
    },
    component: () => import('../views/business/DatabaseManagement.vue')
  },
  {
    path: '/networkSearchList',
    name: 'NetworkSearchList',
    meta:{
      title: "网络发现设备列表",
      requireAuth:true
    },
    component: () => import('../views/NetworkSearchList.vue')
  },
  {
    path: '/auditLog',
    name: 'AuditLog',
    meta:{
      title: "审计日志列表",
      requireAuth:true
    },
    component: () => import('../views/Log/AuditLog.vue')
  },
  {
    path: '/loginLog',
    name: 'LoginLog',
    meta:{
      title: "登录日志列表",
      requireAuth:true
    },
    component: () => import('../views/Log/LoginLog.vue')
  }
  // ,
  // {
  //   path: '/knowledgeBaseApproval',
  //   name: 'KnowledgeBaseApproval',
  //   meta:{
  //     title:"知识库审批"
  //   },
  //   component: () => import(/* webpackChunkName: "about" */ '../views/knowledgeBase/KnowledgeBaseApproval.vue')
  // }
];

var cookie = {
  set:function(key,val,time){//设置cookie方法
    var date=new Date(); //获取当前时间
    var expiresHours=time;  //将date设置为n小时以后的时间
    date.setTime(date.getTime()+expiresHours*3600*1000); //格式化为cookie识别的时间
    document.cookie=key + "=" + val +";expires="+date.toGMTString();  //设置cookie
  },
  get:function(key){//获取cookie方法
    /*获取cookie参数*/
    var getCookie = document.cookie.replace(/[ ]/g,"");  //获取cookie，并且将获得的cookie格式化，去掉空格字符
    var arrCookie = getCookie.split(";")  //将获得的cookie以"分号"为标识 将cookie保存到arrCookie的数组中
    var tips;  //声明变量tips
    for(var i=0;i<arrCookie.length;i++){   //使用for循环查找cookie中的tips变量
      var arr=arrCookie[i].split("=");   //将单条cookie用"等号"为标识，将单条cookie保存为arr数组
      if(key==arr[0]){  //匹配变量名称，其中arr[0]是指的cookie名称，如果该条变量为tips则执行判断语句中的赋值操作
        tips=arr[1];   //将cookie的值赋给变量tips
        break;   //终止for循环遍历
      }
    }
    return tips;
  }
}

const router = new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes
});
var userInfo={
  login:function(){
    axios({
      method:"post",
      url:"/user/login.action",
      data:{
        username:"admin",
        password:"admin"
      }
    }).then(res=>{
      if(res.data.status){
        let userRole = res.data.content.loginResult.role;
        let role_list = res.data.content.loginResult.role_list;
        let userid = res.data.content.loginResult.user_id;
        let username = res.data.content.loginResult.username;

        sessionStorage.setItem("userRole",userRole);
        sessionStorage.setItem("roleList",role_list);
        sessionStorage.setItem("userid",userid);
        sessionStorage.setItem("username",username);

        console.log( sessionStorage.getItem("role"));
      }else{
        console.log( "login error ..");
      }

    } )//then

  }//login
}

router.beforeEach((to,from,next) =>{
  const url = new URL(window.location)


//userInfo.set();
//userInfo.login();


  if(url.searchParams.has("token")&&!cookie.get('token')){
    cookie.set("token",url.searchParams.get("token"),2)
  }
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if(cookie.get('token')){
      axios.get(global.verifyUrl +'/rest/auth/verify?token='+cookie.get('token'))
          .then(function (response) {
            if(response.data.code==-1){
              cookie.set('token','',-1)
              window.location.href = global.verifyUrl+"/?ReturnUrl=" + window.location.href;
            }
            if(!sessionStorage.getItem("roleList")){
              userInfo.login();
            }
            localStorage.setItem("username", response.data.data.username)
            localStorage.setItem("userid", response.data.data.id)
            localStorage.setItem("token", cookie.get('token'))
            localStorage.setItem("role", response.data.data.role)
            localStorage.setItem("permissions", response.data.data.permissions)
            // if(global.isManager("S003"))//此处syscode根据实际系统填写
            if(global.isManager("S002"))//此处syscode根据实际系统填写
              next(); //成功则跳转
            else
              window.location.href= global.eip;//+"/?ReturnUrl=" + window.location.href
          })
    }else {
      window.location.href= global.verifyUrl+"/?ReturnUrl=" + window.location.href
    }
  }
  else {
    document.title = to.matched[0].meta.title;
    next();
  }

})


export default router
